package com.zhou;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * T5
 *
 * @author zhouxianfeng
 * @date 2021-8-17
 */
public class T6 {

    private static Node root = new Node();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] s = in.nextLine().split(" ");
        int libNum = Integer.parseInt(s[0]);
        int problemNum = Integer.parseInt(s[1]);
        List<String> lib = new ArrayList<>();
        for (int i = 0; i < libNum; i++) {
            addNode(root, in.nextLine());
        }
        List<Node> problems = new ArrayList<>();
        for (int i = 0; i < problemNum; i++) {
            problems.add(Query(root, in.nextLine()));
        }
        problems.forEach(t -> {
            System.out.println(t.index + " " + t.count);
        });

    }

    public static void addNode(Node node, String num) {
        for (int i = 0; i < num.length(); i++) {
            node.count += 1;
            char c = num.charAt(i);
            int listIndex = (int) c - 48;
            Node next = node.list[listIndex];
            if (next == null) {
                next = new Node(c, 0);
                next.index = (i + 1);
                node.list[listIndex] = next;
            }
            node = next;
        }
        node.count += 1;
    }

    public static Node Query(Node node, String num) {
        int i = 0;
        for (; i < num.length(); i++) {
            char c = num.charAt(i);
            int listIndex = (int) c - 48;
            Node next = node.list[listIndex];
            if (next == null) {
                break;
            }
            node = next;
        }
        return node;
    }


    public static class Node {
        Character curr;
        int count;
        int index;
        Node[] list = new Node[10];

        Node(char curr, int count) {
            this.curr = curr;
            this.count = count;
        }

        Node() {

        }
    }
}
